package com.musicapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class ItemAdapter3 extends RecyclerView.Adapter<ItemAdapter3.ViewHolder3> {
    private List<ItemModel> itemList;
    private OnItemClickListener mOnItemClickListen;

    public ItemAdapter3(List<ItemModel> itemList){
        this.itemList = itemList;
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public void setmOnItemClickListen(OnItemClickListener onItemClickListen){
        this.mOnItemClickListen = onItemClickListen;
    }

    @NonNull
    @Override
    public ViewHolder3 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.tuijian,parent,false);
        return new ViewHolder3(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder3 holder, int position) {
        ItemModel item = itemList.get(position);
        holder.bind(item);
        if(mOnItemClickListen!=null){
            holder.imageView1.setOnClickListener(v->mOnItemClickListen.onItemClick(v,position));
            holder.addList.setOnClickListener(v -> mOnItemClickListen.onItemClick(v,position));
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public static class ViewHolder3 extends RecyclerView.ViewHolder{
        public ImageView imageView1;
        public ImageView addList;
        public ViewHolder3(@NonNull View itemview){
            super(itemview);
            imageView1 = itemview.findViewById(R.id.image);
            addList = itemview.findViewById(R.id.add);
        }
        public void bind (ItemModel itemModel){
            Glide.with(itemView.getContext())
                    .load(itemModel.getCoverImage())
                    .into(imageView1);
        }
    }
}